///照片选择
///
import 'dart:convert';

import 'package:flutter/material.dart';

import '../bean/AbstactCardContainerBean.dart';
import '../res/HDColors.dart';
import 'CardContainerVertical.dart';
import 'file_picker_widget.dart';
import 'image_picker_widget.dart';


///表示简单的选择 是否 之类的

class FieldFile extends FormField implements CardContainerBean {
  @override
  bool? autoValidate;

  @override
  Widget? content;

  @override
  String? errorText;

  @override
  bool? isRequired;

  @override
  String? label;

  @override
  String? hintLabel;

  @override
  MainAxisAlignment? labelAlign;

  @override
  double? labelIconSize;

  @override
  TextStyle? labelStyle;

  @override
  Widget? labelSuffix;

  @override
  double? labelWitch;

  @override
  Icon? leftIcon;

  @override
  Icon? rightIcon;

  @override
  bool? showLine;

  @override
  bool? visible;

  // @override

  FormFieldSetter? onSaved;

  Widget? rightContent;

  FilePickerController filePickerController;

  // //
  //  @override
  //  FormFieldValidator<String>? validator;

  final List<ImagePickerData> initialValue;

  final List<String> listData;

  final ValueChanged<Map>? onChanged;

  FieldFile({
    Key? key,
    this.content,
    this.errorText,
    required this.label,
    required this.listData,
    this.labelAlign,
    this.labelIconSize,
    this.labelStyle,
    this.labelSuffix,
    this.labelWitch,
    this.leftIcon,
    this.rightIcon,
    this.showLine = true,
    this.visible,
    required this.initialValue,
    this.onSaved,
    // this.validator,
    this.onChanged,
    this.isRequired,
    this.readOnly,
    this.rightContent,
    required this.filePickerController,
  }) : super(
            key: key,
            builder: (field) {
              return (field as _FieldFileState)._build(field.context);
            });

  @override
  _FieldFileState createState() => _FieldFileState();

  @override
  bool? readOnly;
}

class _FieldFileState extends FormFieldState {
  dynamic selectValue = {};
  FilePickerController mcontrol = FilePickerController();

  @override
  FieldFile get widget => super.widget as FieldFile;

  @override
  void initState() {
    //setState(() {
    selectValue = widget.initialValue;
    mcontrol = widget.filePickerController;
    //
    //})
    _initdata();
    super.initState();
  }

  @override
  void didUpdateWidget(FieldFile oldWidget) {
    super.didUpdateWidget(oldWidget);
    //_initdata();
  }

  //初始化处理 数据
  _initdata() {
    List<ImagePickerData> listattch = [];
    mcontrol.resetFiles([]);
    // if (selectValue != null && selectValue != "") {
    //   try {
    //     var listmap = (selectValue is String)?json.decode(selectValue):selectValue;//;
    //     if (listmap is Map) {
    //       if (listmap["attachList"] is List) {
    //         listmap["attachList"].forEach((element) {
    //           //传输-1表示需要根据名字自己去换取url
    //           listattch.add(FilePickerData(
    //               name: element["objectName"], path: '', fileName: element["fileName"],id:0));
    //         });
    //       }
    //     }
    //   } catch (e) {
    //     selectValue.split(",").forEach((element) {
    //       listattch.add(new FilePickerData(fileName: element, path: "", name: element,id:0));
    //     });
    //   }
    // }
    //表示把图片赋值到图片控件上
    mcontrol.addFiles(selectValue);
  }

  @override
  void reset() {
    super.reset();
    setState(() {
      selectValue = widget.initialValue;
    });
    // didChange(selectValue);
    // widget.onChanged?.call({
    //   //widget.chooseData.keys.elementAt(indexSelect):
    //   //widget.chooseData.values.elementAt(indexSelect)
    // });
  }

  @override
  void save() {
    // if(mcontrol.getFiles().isNotEmpty){
    //   setValue(mcontrol.getFiles().map((e) => e.name).toList().join(","));
    // }
    var strvalue;
    if (mcontrol.getFiles().isNotEmpty && mcontrol.getFiles().length > 0) {
      List tempdata = mcontrol.getFiles().where((element) => element.isDel == false).toList();
      if (tempdata.length > 0) {
        //strvalue =  json.encode(
        strvalue =   {
          "attachList": tempdata
              .map((e) => {
                    "fileName": e.fileName ??
                        e.name.toString().split("/")[e.name.toString().split("/").length - 1],
                    "objectName": e.name
                  })
              .toList()
        };
        //);
      }
    }
    setValue(strvalue);

    super.save();
  }

  _build(BuildContext context) {
    return CardContainerVertical(
      label: widget.label,
      labelWitch: widget.labelWitch,
      labelAlign: widget.labelAlign,
      labelIconSize: widget.labelIconSize,
      labelStyle: widget.labelStyle,
      leftIcon: widget.leftIcon,
      autoValidate: widget.autoValidate,
      visible: widget.visible ?? true,
      rightIcon: widget.readOnly ?? false
          ? null
          : Icon(color: HDColors.formTitleColor, Icons.keyboard_arrow_right),
      labelSuffix: widget.labelSuffix,
      showLine: widget.showLine,
      rightClick: (value) {
        if (!(widget.readOnly ?? false)) {
          mcontrol.openChoseFiles();
        }
      },
      isRequired: widget.isRequired,
      rightContent: widget.rightContent,
      content: FilePickerWidget(
        hidehead: true,
        readonly: widget.readOnly,
        controller: mcontrol,
      ),
    );
  }
}
